package com.qixiaobao.system.service.impl;

import com.qixiaobao.common.annotation.datasource.Slave;
import com.qixiaobao.common.annotation.datasource.Third;
import com.qixiaobao.common.core.domain.entity.SysUser;
import com.qixiaobao.common.utils.SecurityUtils;
import com.qixiaobao.system.mapper.SysUserMapper;
import com.qixiaobao.system.service.ISysUserService;
import org.passay.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户 业务层处理
 */
@Slave
@Service
public class SysUserServiceImpl implements ISysUserService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    /**
     * 修改用户密码
     *
     * @param username 用户名
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     */
    @Override
    public int changePassword(String username, String oldPassword, String newPassword) {
        int row;
        SysUser u = userMapper.selectUserByUserName(username);
        if (u == null) {
            throw new IllegalArgumentException("用户不存在");
        }
        if (!SecurityUtils.matchesPassword(oldPassword, u.getPassword())) {
            throw new IllegalArgumentException("旧密码错误");
        }
        //密码校验规则
        PasswordValidator validator = getPasswordValidator();
        RuleResult ruleResult = validator.validate(new PasswordData(newPassword));
        //密码校验通过
        if (ruleResult.isValid()){
            row = userMapper.resetUserPwd(u.getId(), SecurityUtils.encryptPassword(newPassword));
        }else {
            throw new RuntimeException("密码修改失败，不符合密码规范：8位数，须包含字母大小写、特殊字符、数字");
        }
        log.debug("修改{}的密码", username);
        return row;
    }

    /**
     * 获取密码校验器
     * @return
     */
    private PasswordValidator getPasswordValidator() {
        PasswordValidator validator  = new PasswordValidator(
                new LengthRule(8, 20),
                new CharacterCharacteristicsRule(4,
                        //至少有一个大写字母
                        new CharacterRule(EnglishCharacterData.UpperCase, 1),
                        //至少有一个小写字母
                        new CharacterRule(EnglishCharacterData.LowerCase, 1),
                        //至少有一个数字
                        new CharacterRule(EnglishCharacterData.Digit, 1),
                        //至少有一个特殊字符
                        new CharacterRule(EnglishCharacterData.Special, 1)
                ),
                //不允许包含空格
                new WhitespaceRule()
        );
        return validator;
    }

    /**
     * @Description 获取BI端用户ID列表
     * @Author yangxm
     * @Date 2025/7/2 10:03
     * @return java.util.List<com.qixiaobao.common.core.domain.entity.SysUser>
     */
    @Third
    @Override
    public List<Long> selectBiUserIdList() {
        return userMapper.selectBiUserIdList();
    }

}
